## Clayton, Horrillo, and Sniderman 
## The BIAT and the AMP As Measures of Racial Prejudice in Political Science: A Methodological Assessment
## Appendix: Data analysis and visualization (W9 AMP participants only)


# Initial settings --------------------------------------------------------

rm(list = ls())
library(tidyverse)

# Read data ---------------------------------------------------------------

panel <- read.csv("output/w9app_panel.csv") 


# Figure 1: Correlation between AMP and IAT -------------------------------

# Get rid of infinite values
panel$IAT_D[panel$IAT_D=="Inf"] <- NA
panel$IAT_D[panel$IAT_D=="-Inf"] <- NA

# Destring IAT data
panel$IAT_D <- as.numeric(panel$IAT_D)

# Run OLS regression
mod <- summary(lm(diff~IAT_D, panel))

# Create labelling shortcuts for model attributes
beta <- round(mod$coefficients[2,1], 3)
rsq <- round(mod$r.squared, 3)

# Position templates
xIntercept <- max(panel$diff)
yIntercept <- max(na.omit(panel$IAT_D))

# Text to be parsed in annotation 
beta_note <- paste("beta == ", beta)
rsq_note <- paste("italic(R)^2 == ", rsq)

# IAT vs AMP scatter
iat.amp <- ggplot(panel, aes(x = IAT_D, y = diff)) +
  geom_point(alpha = 0.1) + 
  geom_smooth(method = "lm",
              formula = "y ~ x") + 
  geom_hline(yintercept = 0, linetype = "dashed") +
  geom_vline(xintercept = 0, linetype = "dashed") +  
  annotate("text", x = (xIntercept + 0.235), y = yIntercept, label = beta_note, color = "black", parse = TRUE, size=6) +
  annotate("text", x = (xIntercept + 0.2), y = (yIntercept-0.11), label = rsq_note, color = "black", parse = TRUE, size=6) +
  theme_light() + 
  labs(x = "BIAT-D score", 
       y = "AMP score")
iat.amp
ggsave("figures/FigureB1.png", iat.amp, width = 8, height = 6)
